<template>
  <a-modal
    title="编辑用户信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="账户"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账户" v-decorator="['account', {rules: [{required: false, message: '请输入账户！'}]}]" />
        </a-form-item>
        <a-form-item
          label="电话"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入电话" v-decorator="['phone', {rules: [{required: false, message: '请输入电话！'}]}]" />
        </a-form-item>
        <a-form-item
          label="地址"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入地址" v-decorator="['address', {rules: [{required: false, message: '请输入地址！'}]}]" />
        </a-form-item>
        <a-form-item
          label="年龄"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入年龄" v-decorator="['age', {rules: [{required: false, message: '请输入年龄！'}]}]" />
        </a-form-item>
        <a-form-item
          label="性别(0-未知，1-男，3-女)"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入性别(0-未知，1-男，3-女)" v-decorator="['sex', {rules: [{required: false, message: '请输入性别(0-未知，1-男，3-女)！'}]}]" />
        </a-form-item>
        <a-form-item
          label="收入"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收入" v-decorator="['income', {rules: [{required: false, message: '请输入收入！'}]}]" />
        </a-form-item>
        <a-form-item
          label="行业"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入行业" v-decorator="['industry', {rules: [{required: false, message: '请输入行业！'}]}]" />
        </a-form-item>
        <a-form-item
          label="昵称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入昵称" v-decorator="['nickname', {rules: [{required: false, message: '请输入昵称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="头像"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入头像" v-decorator="['headPortrait', {rules: [{required: false, message: ''}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { userEdit } from '@/api/modular/main/user/userManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        createtimeDateString: '',
        updatetimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              account: record.account,
              phone: record.phone,
              password: record.password,
              createuser: record.createuser,
              address: record.address,
              age: record.age,
              sex: record.sex,
              income: record.income,
              industry: record.industry,
              nickname: record.nickname,
              headPortrait: record.headPortrait,
              updateuser: record.updateuser
            }
          )
        }, 100)
        // 时间单独处理
        if (record.createtime != null) {
            this.form.getFieldDecorator('createtime', { initialValue: moment(record.createtime, 'YYYY-MM-DD') })
        }
        this.createtimeDateString = moment(record.createtime).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.updatetime != null) {
            this.form.getFieldDecorator('updatetime', { initialValue: moment(record.updatetime, 'YYYY-MM-DD') })
        }
        this.updatetimeDateString = moment(record.updatetime).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.createtime = this.createtimeDateString
            values.updatetime = this.updatetimeDateString
            userEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangecreatetime(date, dateString) {
        this.createtimeDateString = dateString
      },
      onChangeupdatetime(date, dateString) {
        this.updatetimeDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
